﻿using System;
using System.Windows.Forms;
using Nortec.Security;
using Nortec.Security.BusinessComponents;
using Nortec.Security.BusinessEntities;
using System.Configuration;

namespace Litografia.UI.Seguridad
{
    public partial class LoginForm : BaseForm
    {
        string applicationName;

        public LoginForm()
        {
            InitializeComponent();
        }

        private void Login_Load(object sender, EventArgs e)
        {
            BCSecurity securityObject = new BCSecurity();
            applicationName = ConfigurationManager.AppSettings["ApplicationName"];

            BEEmpresa.EmpresaDataTable empresas = securityObject.GetCompanies(applicationName);
            cmbCompany.ValueMember = "lit_empresa_id";
            cmbCompany.DisplayMember = "lit_empresa_nombre";
            cmbCompany.DataSource = empresas;

            int companyId = Convert.ToInt32(cmbCompany.SelectedValue);
            BESucursal.SucursalDataTable sucursales = securityObject.GetBranches(companyId);
            cmbBranch.ValueMember = "lit_sucursal_id";
            cmbBranch.DisplayMember = "lit_sucursal_nombre";
            cmbBranch.DataSource = sucursales;

            toolStrip.Visible = false;
        }

        private void cmbCompany_SelectedIndexChanged(object sender, EventArgs e)
        {
            BCSecurity securityObject = new BCSecurity();
            int companyId = Convert.ToInt32(cmbCompany.SelectedValue);
            BESucursal.SucursalDataTable sucursales = securityObject.GetBranches(companyId);
            cmbBranch.ValueMember = "lit_sucursal_id";
            cmbBranch.DisplayMember = "lit_sucursal_nombre";
            cmbBranch.DataSource = sucursales;
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtUserName.Text.Trim()))
                ShowError("Por favor digite su nombre de usuario");
            else if (String.IsNullOrEmpty(txtPassword.Text.Trim()))
                ShowError("Por favor digite su contraseña");
            else if (cmbCompany.SelectedItem == null)
                ShowError("Por favor seleccione la compañía en la que labora");
            else if (cmbBranch.SelectedItem == null)
                ShowError("Por favor seleccione la sucursal en la que labora");
            else
            {
                int companyId = Convert.ToInt32(cmbCompany.SelectedValue);
                string companyName = cmbCompany.Text;
                int branchId = Convert.ToInt32(cmbBranch.SelectedValue);
                string branchName = cmbBranch.Text;

                NortecMembership.Initialize(companyId, companyName, branchId, branchName, applicationName);

                if (NortecMembership.ValidateUser(txtUserName.Text, txtPassword.Text))
                {
                    var identity = new NortecIdentity(txtUserName.Text.Trim(), "NortecMembership", NortecMembership.ApplicationId);
                    var principal = new NortecPrincipal(identity);
                    NortecMembership.Principal = principal;
                    NortecMembership.Identity = identity;
                    DialogResult = DialogResult.OK;
                    Close();
                }
                else ShowError("Nombre de usuario o contraseña inválida. Intente de nuevo");
            }
        }
    }
}
